package com.xbang.attendance.web.action.system.sysparam;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.xbang.attendance.entity.system.Region;
import com.xbang.attendance.entity.system.SysParam;
import com.xbang.attendance.entity.system.Warehouse;
import com.xbang.attendance.entity.system.WarehouseProductAgreement;
import com.xbang.attendance.service.SystemService;
import com.xbang.attendance.web.action.system.setuphouse.WarehouseManager;
import com.demo2do.lighturl.annotation.Action;
import com.xbang.core.page.Result;
import com.xbang.core.struts.support.SearchActionSupport;

public class SysParamManager extends SearchActionSupport {

	private static final long serialVersionUID = 6843098369301424790L;

	private static final Log logger = LogFactory.getLog(WarehouseManager.class);

	private Integer id;
	
	@Autowired
	private SystemService systemService;
	
	private SysParam sysparam;
	
	private List<SysParam> sysParams = new ArrayList<SysParam>();
	
	@Action("/system/sysparam/${id}/edit")
	public String edit() throws Exception {
		sysparam = (SysParam) systemService.getObject(SysParam.class, id);
		return "detail";
	}
	
	@Action("/system/sysparam/list")
	public String list() throws Exception {

		String hqlSentence = "FROM SysParam sysparam WHERE 1 = 1";
		if (searchComponent.getOrderParameters().isEmpty()){
			searchComponent.addSearchElement("orderBy.sysparam.name", "ASC");
		}
		Result result = systemService.searchByPage(hqlSentence, searchComponent, page);

		sysParams = result.getItems();
		page = result.getPage();

		return "index";
	}
	
	@Action("/system/sysparam/save")
	public String save() throws Exception {
		if (sysparam.getId() != null) {
			
			SysParam p = (SysParam) systemService.get(SysParam.class, sysparam.getId());
			
			p.setName(sysparam.getName());
			p.setValue(sysparam.getValue());
			
			systemService.update(p);
		}
		return "r:/system/sysparam/list";
	}
	
	@Override
	public void resetCustomerSearchCriteria() {
		// TODO Auto-generated method stub
		
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public SystemService getSystemService() {
		return systemService;
	}

	public void setSystemService(SystemService systemService) {
		this.systemService = systemService;
	}

	public SysParam getSysparam() {
		return sysparam;
	}

	public void setSysparam(SysParam sysparam) {
		this.sysparam = sysparam;
	}

	public List<SysParam> getSysParams() {
		return sysParams;
	}

	public void setSysParams(List<SysParam> sysParams) {
		this.sysParams = sysParams;
	}
	

}
